//
//  HMMainTabBar.swift
//  weibo2
//
//  Created by 仰望星空 on 16/7/19.
//  Copyright © 2016年 仰望星空. All rights reserved.
//

import UIKit

class HMMainTabBar: UITabBar {

    //闭包属性
    var composeCallback:(()->Void)?
    //懒加载按钮
    lazy var composeButton:UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        //设置按钮图片
        button.setImage(UIImage(named:"tabbar_compose_icon_add" ), forState: UIControlState.Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted" ),forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal);
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        //添加到TabBar上面
        self.addSubview(button)
        
        //给按钮添加点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print("layoutSubviews")
        //按钮数量
        let itemCount = 5
        
        
        
        //计算每个按钮的宽度
        let newItemWith = self.frame.width/CGFloat(itemCount)
        
        var index = 0
        
        //遍历所有的item(我们知道TabBar里面的子控件subviews就是item，注意这里面有多个item，有些是不需要的，所以需要区别对待)
        let cls = NSClassFromString("UITabBarButton")
        
        for item in subviews{
            
            if item.isKindOfClass(cls!){
                
                //重新设置item的frame
                item.frame = CGRect(x: newItemWith * CGFloat(index) , y:0, width: newItemWith, height: self.frame.height)
                
                index++
                
                if index == 2{
                    index++
                    
                }
                
            }
        
        }
        
        //设置composeButton的frame值
        composeButton.frame = CGRect(x: newItemWith * 2, y: 0, width: newItemWith, height: self.frame.height)
        
        

    }
    
    //MARK: - 按钮点击事件
    func composeClick(){
        
        print("composeClick")
        
    }
    
    
    
    
    
}
